import {ajax, cardCache, deepClone ,formDownload,toast} from 'nc-lightapp-front'; 
import { CARD_BUTTON, CARD, STATUS, CARD_DISABLED_BUTTON, PRIMARY_KEY,REQUEST_URL } from '../../constant';

/**
* @description: 表体按钮点击事件
* @param: key 当前触发按钮名称
* @param: index 当前行序
* @param: record 当前行信息
*/
export function bodyButtonClick(props, key, text, record, index) {

    switch (key) {
        //肩 新增
        case CARD_BUTTON.addRow:
            let rowCount = props.cardTable.getNumberOfRows(CARD.table_code);
            props.cardTable.addRow(CARD.table_code);
            props.cardTable.setValByKeyAndIndex(CARD.table_code, rowCount, 'sno', { display: null, scale: null, value:  (rowCount +1 ) * 10 + '', isEdit: true });  
           
            break;

        //肩 删除
        case CARD_BUTTON.deleteRow:
            let checkedRows = props.cardTable.getCheckedRows(CARD.table_code);
            let checkedIndex = checkedRows && checkedRows.map(item => item.index);
            if (checkedRows.length > 0) {
                props.cardTable.delRowsByIndex(CARD.table_code, checkedIndex);
                props.button.setButtonDisabled(CARD_DISABLED_BUTTON, true);
            }
            break;

        //行 展开
        case CARD_BUTTON.expand:
            //侧拉展开适配
            props.cardTable.openModel(CARD.table_code, STATUS.edit, record, index);
            break;

        //行 展开（浏览态）
        case CARD_BUTTON.unfold:
        //行 收起
        case CARD_BUTTON.fold:
            //适配
            props.cardTable.toggleRowView(CARD.table_code, record);
            break;

        //行 插行
        case CARD_BUTTON.insertRow:
            props.cardTable.addRow(CARD.table_code, index);
            break;

        //行 删行
        case CARD_BUTTON.delRow:
            props.cardTable.delRowsByIndex(CARD.table_code, index);
            break;

        //行 复制行
        case CARD_BUTTON.copyRow:
            props.cardTable.pasteRow(CARD.table_code, index, [PRIMARY_KEY.body_id]);
            break;

        //肩 复制行
        case CARD_BUTTON.copyRows:
            copyControl.call(this, props, true);
            break;

        //肩 取消复制
        case CARD_BUTTON.pasteCancel:
            copyControl.call(this, props, false);
            break;

        //肩 粘贴至末行
        case CARD_BUTTON.pasteTail:
            let lastIndex = props.cardTable.getNumberOfRows(CARD.table_code);
            props.cardTable.insertRowsAfterIndex(CARD.table_code, getPasteRows(props), lastIndex);
            copyControl.call(this, props, false);
            props.cardTable.selectAllRows(CARD.table_code, false);
            break;

        //行 粘贴至此
        case CARD_BUTTON.pasteHere:
            props.cardTable.insertRowsAfterIndex(CARD.table_code, getPasteRows(props), index);
            copyControl.call(this, props, false);
            props.cardTable.selectAllRows(CARD.table_code, false);
            break;
        case CARD_BUTTON.DownTempletGra:
            downTemplet.call(this,props);
            break
        case CARD_BUTTON.UploadGra:
            UploadGra.call(this,props);
            break

    }
    if (![CARD_BUTTON.unfold, CARD_BUTTON.fold].includes(key)) {
        props.cardTable.setStatus(CARD.table_code, STATUS.edit);
    }
}

function UploadGra(props){
    document.querySelector('#fileInfo').click();
}

export function importSuccess(data){
    this.props.cardTable.setTableData(CARD.table_code, { rows: data['retData'].body.card_body.rows });
    
    if(data.errorMessage == undefined){
        toast({ content: '导入成功', color: 'success' });
    }else{
        toast({content:data.errorMessage,color:'da9nger'});//错误消息
    }

}

function downTemplet(props){
    let colunmNames = null;
    let fileNames = null;
    
            colunmNames = ['序号','员工号','姓名','GHR机构编码','GHR机构','GHR缴金法人编码','缴金法人单位','GHR实际服务法人编码','实际服务法人','险种','GHR一级部门编码','GHR一级部门名称','GHR二级部门编码','GHR二级部门名称','岗位编码','岗位中文名称','缴费工资','社保公司承担','社保个人承担','社保合计','单位承担养老基数','单位承担养老比例','单位承担养老金额','单位承担失业基数','单位承担失业比例','单位承担失业金额','单位承担工伤基数','单位承担工伤比例','单位承担工伤金额','单位承担生育基数','单位承担生育比例','单位承担生育金额','单位承担医疗基数','单位承担医疗比例','单位承担医疗大病救助','单位承担医疗金额','单位承担公司合计','个人承担养老基数','个人承担养老比例','个人承担养老金额','个人承担失业基数','个人承担失业比例','个人承担失业金额','个人承担医疗基数','个人承担医疗比例','个人承担医疗大病救助','个人承担医疗金额','个人承担公司合计','派遣服务费','档案管理费','公积金基数','公积金公司承担比例','公积金公司承担金额','公积金个人承担比例','公积金个人承担金额','公积金合计'];
            fileNames = ['社保、公积金、劳动管理费']
    

    // let colunmNames = ['利润中心','明细摘要','金额'];
    let data = {
            colunmNames: colunmNames,
            fileNames: fileNames
    }

    formDownload({
            params: data,
            url: REQUEST_URL.DownTempletGra,
            enctype: 2
    });
    toast({ content: '导出成功', color: 'success' });
}

//复制时的状态控制
function copyControl(props, flag) {
    //肩部按钮显示控制
    props.button.setButtonVisible({
        [CARD_BUTTON.addRow]: !flag,
        [CARD_BUTTON.deleteRow]: !flag,
        [CARD_BUTTON.copyRows]: !flag,

        [CARD_BUTTON.pasteTail]: flag,
        [CARD_BUTTON.pasteCancel]: flag,
    });
    props.cardTable.setAllCheckboxAble(CARD.table_code, !flag);
    this.setState({ isPaste: flag });
}

/**
 * 获取粘贴行数据
 *
 * @returns 返回粘贴行数据
 */
function getPasteRows(props) {
    let checkedRows = props.cardTable.getCheckedRows(CARD.table_code);
    let selectRowCopy = deepClone(checkedRows);
    let selectArr = selectRowCopy.map(item => {
        item.data.selected = false;
        item.data.values[PRIMARY_KEY.head_id] ? item.data.values[PRIMARY_KEY.head_id].value = null : null;
        item.data.values[PRIMARY_KEY.body_id] ? item.data.values[PRIMARY_KEY.body_id].value = null : null;
        return item.data;
    });
    return selectArr;
}
